public class Solution {
    public int integerReplacement(int n) {
        if (n == Integer.MAX_VALUE) return 32;
        int count = 0;
        while (n > 1) {
            if((n & 1) == 0) {
                n = n / 2;
            } else if((n & 3) == 3 && n != 3) {
                n = n + 1;
            } else {
                n = n - 1;
            }
            count++;
        }
        
        return count;
    }
    
    public static void main(String[] args) {
        System.out.println(new Solution().integerReplacement(2147483647)); 
    }
}
